function getCookie (name: string): string | null {
  let arr: RegExpMatchArray | null
  const req: RegExp = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  // eslint-disable-next-line prefer-const
  arr = document.cookie.match(req)
  if (arr) {
    return unescape(arr[2])
  } else {
    return null
  }
}

function setCookie (key: string, value: string | null, day: number = 0): void {
  const date: Date = new Date()
  date.setTime(date.getTime() + day * 24 * 3600 * 1000)
  document.cookie = `${key}=${value};expires=${date.toUTCString()};path=/`
}

function delCookie (key: string): void {
  const date: Date = new Date()
  date.setTime(date.getTime() - 1)
  const delValue: boolean = getCookie(key) !== null
  if (delValue) {
    document.cookie = `${key}=${delValue};expires=${date.toUTCString()};path=/`
  }
}

export default {
  getCookie,
  setCookie,
  delCookie
}
